var table = layui.table;
layui.use(['form','layer'],function() {
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    layui.use('table', function () {
        tableIns=table.render({
            elem: '#roleList'
            , url: '/sysRoleController/selectRole'
            , method: 'post' //默认：get请求
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'limit' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.totalRec, //解析数据长度
                    "data": res.data.recList //解析数据列表
                };
            }
            , cellMinWidth: 95
            , cols: [[
                {type: 'numbers', title: '编号'}
                , {type: 'checkbox'}
                , {field: 'name', title: '角色名称', unresize: true, sort: true}
                , {field: 'title', title: '角色标题', unresize: true, sort: true}
                , {field: 'descr', title: '角色描述', unresize: true, sort: true}
                , {field: 'createTime', title: '添加时间', unresize: true, sort: true, align: 'center',}
                , {fixed: 'right', title: '操作', width: 300, align: 'center', toolbar: '#roleListBar'}
            ]]
            , page: true
            , id: 'roleListTable'
        });
    });
    layui.use('table', function () {
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(roleList)', function (obj) {
            console.log(obj)
        });
        //监听工具条
        table.on('tool(roleList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'tableUpdate') {
                window.UpdateId = data.id;
                layer.open({
                    type: 2,
                    title: '角色信息',
                    content: '/sysRoleController/toUpdateRole?id=' + data.id,
                    shade: 0.5,
                    area : ["500px","320px"],
                    maxmin: true,
                    end: function(){
                        table.reload('roleListTable');
                    }
                });
            }
            else if (obj.event === 'tableDelete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    $.ajax({
                        type: "post",
                        url: "/sysRoleController/deleteRole?id=" + data.id,
                        dataType: "json"
                    });
                    layer.close(layer.index);
                    var index = parent.layer.getFrameIndex(window.name);
                    layer.close(index);
                    layui.table.reload('roleListTable');
                });
            }else if(obj.event === 'grant'){
                layer.open({
                    type: 2,//1.弹层，2.引入页面
                    area:["350px", "500px"],
                    tips:3,
                    closeBtn: 1,
                    moveType: 1, //拖拽模式，0或者1
                    content : "/dtree/index?roleId="+data.id
                });
            }
        });
    });
});
layui.jquery('#searchTransport').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
});
var active ={
    reload: function(){
        var demoReload = $('#demoReload');
        //执行重载
        layui.table.reload('roleListTable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                name: demoReload.val()
            }
        }, 'data');
    }
};
function insert() {
    layer.open({
        type: 2,
        title: '添加角色',
        content: '/sysRoleController/toAddRole',
        shade: 0.5,
        area : ["500px","320px"],
        maxmin: true,
        end: function(){
            table.reload('roleListTable');
        }
    });
}
layui.jquery("#delAll_btn").click(function () {
    var checkStatus = layui.table.checkStatus('roleListTable'),
        data = checkStatus.data,
        roleId = [];
    if (data.length > 0) {
        for (var i in data) {
            roleId.push(data[i].id);
        }
        console.log(roleId.join(','));
        layer.confirm('确定删除选中的角色？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/sysRoleController/toDelete", {
                id: roleId.join(',')  //将需要删除的newsId作为参数传入
            }, function (data) {
                layer.msg("角色删除成功！");
                layui.table.reload('roleListTable');
                layer.close(index);
                window.location.reload();
            })
        })
    } else {
        layer.msg("请选择需要删除的角色");
    }
});